﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class AppendScorePage : EventManager
{
    public Text appendScoreTex;
    public Text totalScoreTex;
    public Text serviceChargeTex;
    public Button closeBtn;
    public Button confirmBtn;
    public Slider appendSlider;
    public int appendScore;
    public GameObject toggleGroup;
    public Toggle[] toggleArr;
    private string approver;


    public override void InformationSetting()
    {
        SetText(appendScoreTex, GameRoomModel.GetInstance().MinTakeInScore.ToString());
        SetText(totalScoreTex, GameData.GetInstance().gold.ToString());
        SetText(serviceChargeTex, (GameRoomModel.GetInstance().MinTakeInScore/10).ToString());
        appendSlider.value = 0;
        AddListenerToButton(closeBtn, CloseBtnClick);
        AddListenerToButton(confirmBtn, ConfirmBtnClick);

        for (int i = 0; i < 3; i++)
        {
            toggleArr[i].gameObject.SetActive(false);
        }

        if (GameRoomModel.GetInstance().RoomType == 2 && string.IsNullOrEmpty(GameRoomModel.GetInstance().playerDic[GameData.GetInstance().locationIndex].approver))
        {
            toggleGroup.SetActive(true);
            InitToggle();
            approver = toggleArr[0].transform.FindChild("Label").GetComponent<Text>().text;
        }
        else
        {
            toggleGroup.SetActive(false);
            approver = GameRoomModel.GetInstance().playerDic[GameData.GetInstance().locationIndex].approver;
        }
    }


    private void InitToggle()
    {
        string[] str = GameRoomModel.GetInstance().ClubOrLianMengName.Split(',');

        for (int i = 1; i < str.Length; i++)
        {
            foreach (var item in GameData.GetInstance().clubDic)
            {
                if (item.Key == str[i])
                {
                    SetToggle(item.Key);
                }
            }
        }
    }

    private void SetToggle(string name)
    {
        if (!toggleArr[0].gameObject.active)
        {
            toggleArr[0].transform.FindChild("Label").GetComponent<Text>().text = name;
            toggleArr[0].gameObject.SetActive(true);
        }
        else if (!toggleArr[1].gameObject.active)
        {
            toggleArr[1].transform.FindChild("Label").GetComponent<Text>().text = name;
            toggleArr[1].gameObject.SetActive(true);
        }
        else
        {
            toggleArr[2].transform.FindChild("Label").GetComponent<Text>().text = name;
            toggleArr[2].gameObject.SetActive(true);
        }
    }



    public void Toggle0Click()
    {
        if (toggleArr[0].isOn)
        {
            approver = toggleArr[0].transform.FindChild("Label").GetComponent<Text>().text;
        }
    }

    public void Toggle1Click()
    {
        if (toggleArr[1].isOn)
        {
            approver = toggleArr[1].transform.FindChild("Label").GetComponent<Text>().text;
        }
    }

    public void Toggle2Click()
    {
        if (toggleArr[2].isOn)
        {
            approver = toggleArr[2].transform.FindChild("Label").GetComponent<Text>().text;
        }
    }

    public void ConfirmBtnClick()
    {
        if (appendScore == 0)
            appendScore = GameRoomModel.GetInstance().MinTakeInScore;

        MessageManager.GetInstance().SendMsg1233(appendScore, GameData.GetInstance().locationIndex, approver);

        CloseBtnClick();
    }

    public void OnValueChange()
    {
        double temp = GameRoomModel.GetInstance().MinTakeInScore + appendSlider.value * GameRoomModel.GetInstance().MinTakeInScore * (GameRoomModel.GetInstance().MaxTakeInScore - 1);
        appendScore = ((int)(temp / GameRoomModel.GetInstance().MinTakeInScore)) * GameRoomModel.GetInstance().MinTakeInScore;
        SetText(appendScoreTex, appendScore.ToString());
        SetText(serviceChargeTex, (appendScore/10).ToString());
    }
}
